package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.repository.UserTrajectoryRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.ModuleService;
import com.sl.au.service.UserTrajectoryService;

@Service("userTrajectoryService")
@Transactional
public class UserTrajectoryServiceImpl extends BaseServiceImpl implements UserTrajectoryService {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	UserTrajectoryRepository userTrajectoryRepository;
	
	@Override
	public UserTrajectoryRepository getRepository() {
		return this.userTrajectoryRepository;
	}

	@Override
	public List<String> getOnlineUser(double limit) {
		List<String> onlineUser = new ArrayList<String>();
		String sql = "SELECT b.`user` from ( " + 
				"select a.`user`, MAX(a.trajectoryTime) as `trajectoryTime` from sl_wcsi_UserTrajectory a GROUP BY a.`user` ) b " +
				" where TIME_TO_SEC(TIMEDIFF(now(),b.trajectoryTime)) <= 3600 * "+ limit ;
		onlineUser = jdbcTemplate.queryForList(sql, String.class);
		return onlineUser;
	}

	@Override
	public int saveHistoryTrajectory() {
		String sql = "insert into sl_wcsi_UserTrajectory_history select  * from sl_wcsi_UserTrajectory";
		int count = jdbcTemplate.update(sql);
		return count;
	}
	
}
